﻿using System;
using System.Numerics;
using System.Xml;

namespace VmpRsaDemo
{
    public class AlgorithmRSA : IAlgorithm
    {
        private readonly BigInteger pExponent;
        private readonly BigInteger pModulus;

        public AlgorithmRSA(XmlNode node)
        {
            try
            {
                Bits = int.Parse(node.Attributes["bits"].Value);
                pExponent = new BigInteger(Convert.FromBase64String(node.Attributes["exp"].Value));
                pModulus = new BigInteger(Convert.FromBase64String(node.Attributes["mod"].Value));
            }
            catch (Exception ex)
            {
                throw new ArgumentException("Missed data for RSA algorithm", ex);
            }
        }

        public int Bits { get; private set; }

        public byte[] Encrypt(byte[] data)
        {
            BigInteger src = new BigInteger(data);
            BigInteger res = BigInteger.ModPow(src, pExponent, pModulus);
            return res.ToByteArray();
        }
    }
}